﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Windows;

namespace Passwords
{
    public class CommandHandler
    {
        private Dictionary<int, EventHandler<CommandEventArgs>> handlers = new Dictionary<int, EventHandler<CommandEventArgs>>();

        public CommandHandler(Control control)
            : base()
        {
            control.CommandRaised += OnCommand;
        }

        public void RegisterCommand(Command command, EventHandler<CommandEventArgs> handler)
        {
            int key = command.Id;
            handlers[key] = handler;
        }


        private void OnCommand(object sender, CommandEventArgs e)
        {
            int key = (e.Command as Command).Id;
            EventHandler<CommandEventArgs> handler;
            if (handlers.TryGetValue(key, out handler))
            {
                handler(sender, e);
            }
        }
    }
}
